package com.mh.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.mh.shiro.business.entity.Role;
import com.mh.shiro.business.enums.ResponseStatus;
import com.mh.shiro.business.service.ShiroService;
import com.mh.shiro.business.service.SysRoleResourcesService;
import com.mh.shiro.business.service.SysRoleService;
import com.mh.shiro.business.vo.RoleConditionVO;
import com.mh.shiro.framework.object.PageResult;
import com.mh.shiro.framework.object.ResponseVO;
import com.mh.shiro.util.ResultUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Response;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年06月21日16:14
 * @Description: 系统角色管理
 */
@RestController
@RequestMapping("/roles")
public class RestRoleController {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysRoleResourcesService roleResourcesService;
    @Autowired
    private ShiroService shiroService;

    @RequiresPermissions("user:allotRole")
    @PostMapping("/rolesWithSelected")
    public ResponseVO<List<Role>> rolesWithSelected(Integer uid) {
        return ResultUtil.success(null, roleService.queryRoleListWithSelected(uid));
    }

    @RequiresPermissions("roles")
    @PostMapping("/list")
    public PageResult getAll(RoleConditionVO vo) {
        PageInfo<Role> pageInfo = roleService.findPageBreakByCondition(vo);

        return ResultUtil.tablePage(pageInfo);
    }

    @RequiresPermissions("role:add")
    @PostMapping("/add")
    public ResponseVO add(Role role) {

        roleService.insert(role);

        return ResultUtil.success("成功");
    }

    @RequiresPermissions(value = {"role:batchDelete", "role:delete"}, logical = Logical.OR)
    @PostMapping("/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请选择至少一条记录");
        }
        for (Long id : ids) {
            roleService.removeByPrimaryKey(id);
            roleResourcesService.removeByRoleId(id);
        }
        return ResultUtil.success("成功删除[" + ids.length + "]个角色");
    }

    @RequiresPermissions("role:edit")
    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        Role role = roleService.getByPrimaryKey(id);
        System.out.println(role);

        return ResultUtil.success(null, roleService.getByPrimaryKey(id));
    }

    @RequiresPermissions("role:edit")
    @PostMapping("/edit")
    public ResponseVO edit(Role role) {
        System.out.println(role.getSysRole().getId());

        try {
            roleService.updateSelective(role);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("修改失败");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
    @RequiresPermissions("role:allotResource")
    @PostMapping("/saveRoleResources")
    public ResponseVO saveRoleResources(Long roleId, String resourcesId) {
        if (StringUtils.isEmpty(roleId)) {
            return ResultUtil.error("error");
        }
        roleResourcesService.addRoleResources(roleId, resourcesId);
        //重新加载所有拥有roleId的权限信息
        shiroService.reloadAuthorizingByRoleId(roleId);

        return ResultUtil.success("成功");
    }


}
